<?php
    class Admincp_ImageController extends Main_Controller_Action
    {
        private $_arrParam;
        private $_model;
        private $_locale;
        
        public function init()
        {
            $this->loadTemplates('admin');
            $this->_helper->layout->setLayout('popup');
            $this->view->headTitle('Quản lý ảnh');
            $this->_arrParam = $this->_request->getParams();
            $this->_model = new Admincp_Model_Image();
            $this->_locale = 'vi';
        }
        
        public function preDispatch()
		{
		    $auth = Zend_Auth::getInstance();
		    if(!$auth->hasIdentity()) {
		        if($this->_request->getActionName() != 'login')
		            $this->_redirect('admincp/index/login');
		    }
		}
        
        public function indexAction()
        {
            $type = (int) $this->_arrParam['type'];
            $hotel_id = (int) $this->_arrParam['hotel_id'];
            
            if($_POST['add']) {
                $this->_redirect('admincp/image/create?type='.$type.'&hotel_id='.$hotel_id);
            }
            if($_POST['delete']) {
                $listId = $this->_arrParam['check'];
                $this->_model->deleteMultiItem($listId);
                $this->_redirect('admincp/image/index?type='.$type.'&hotel_id='.$hotel_id);
            }
            
            $data = array(
                'type' => $type,
                'hotel_id' => $hotel_id,
            );

            $adapter = new Zend_Paginator_Adapter_DbSelect($this->_model->listItem($data));
            $paginator = new Zend_Paginator($adapter);
            $paginator->setItemCountPerPage(40);
            $paginator->setPageRange(5);
            $currentPage = $this->_request->getParam('page', 1);
            $paginator->setCurrentPageNumber($currentPage);
            $this->view->image = $data;
            $this->view->data = $paginator;
            $this->view->total = $paginator->getTotalItemCount();
        }
        
        public function createAction()
        {
            $hotel_id = $this->_arrParam['hotel_id'];
            $type = $this->_arrParam['type'];
            $data = array(
                'type' => $type,
                'hotel_id' => $hotel_id,
            );
            $language = new Admincp_Model_Language();
            $this->view->language = $language->listLanguageFull();
            $this->view->image = $data;
            
            if($this->_request->isPost()) {
                $name['vi']     = trim(strip_tags($this->_arrParam['name-vi']));
                $name['zh']     = trim(strip_tags($this->_arrParam['name-zh']));
                $name['en']     = trim(strip_tags($this->_arrParam['name-en']));
                $name['trv']     = trim(strip_tags($this->_arrParam['name-trv']));
                $hotel_id       = $this->_arrParam['hotel_id'];
                $type           = $this->_arrParam['type'];
                $upload         = new Zend_File_Transfer_Adapter_Http();
                $info           = $upload->getFileInfo();
                $image          = $upload->getFileName('image', false);
                
                // Resize Image
                $resize = new Zend_Filter_ImageSize();
                preg_match('#\.([^\.]+)$#', $image, $matches);
                $fileExtension  = $matches[1];
                $fileName = 'pic_' . time() . mt_rand(5, 99999999);
                $newFileName1 = $fileName . '.' . $fileExtension;
                
                $options = array('target' => BASE_PATH . '/public/files/image/' . $newFileName1,'overwrite'=>true);
                $upload->addFilter('Rename',$options,'image');
                $upload->receive('image');
                $output = $resize->setHeight(400)
                                ->setWidth(500)
                                ->setThumnailDirectory(BASE_PATH . '/public/files/image/')
                                ->filter(BASE_PATH . '/public/files/image/' . $newFileName1);
                header('Content-Type: image/jpeg');
                $fh = fopen($output, 'r');
                fpassthru($fh);
                fclose($fh);
                $newFileName = $fileName . '-500x400.' . strtolower($fileExtension);
                unlink(PUBLIC_PATH . '/files/image/' . $newFileName1);
                
                // Insert to database
                $strFlex = new Zendvn_Filter_RemoveCircumflex();
                $data_image = array(
                    'hotel_id' => $hotel_id,
                    'type' => $type,
                    'image' => $newFileName,
                    'status' => 1,
                );
                $image_id = $this->_model->saveItem($data_image);
                foreach($name as $key=>$value) {
                    foreach($this->view->language as $lang) {
                        if($key == $lang['locale']) {
                            $data = array(
                                'image_id' => $image_id,
                                'language_id' => $lang['id'],
                                'name' => $value,
                            );
                            $this->_model->saveItem($data, 'language');
                            break;
                        }
                    }
                }
                $this->_redirect('admincp/image/index?type='.$type.'&hotel_id='.$hotel_id);
            }
        }
        
        public function editAction()
        {
            $id = (int) $this->_arrParam['id'];
            $hotel_id = $this->_arrParam['hotel_id'];
            $type = $this->_arrParam['type'];
            $data = array(
                'type' => $type,
                'hotel_id' => $hotel_id,
            );
            $language = new Admincp_Model_Language();
            $this->view->language = $language->listLanguageFull();
            $this->view->data = $this->_model->getItem($id, 'language');
            $this->view->data2 = $this->_model->getItem($id);
            $this->view->image = $this->view->data2;
            
            if($this->_request->isPost()) {
                $name['vi']     = trim(strip_tags($this->_arrParam['name-vi']));
                $name['zh']     = trim(strip_tags($this->_arrParam['name-zh']));
                $name['en']     = trim(strip_tags($this->_arrParam['name-en']));
                $name['trv']     = trim(strip_tags($this->_arrParam['name-trv']));
                $hotel_id       = $this->_arrParam['hotel_id'];
                $type           = $this->_arrParam['type'];
                $upload         = new Zend_File_Transfer_Adapter_Http();
                $info           = $upload->getFileInfo();
                $image          = $upload->getFileName('image', false);
                
                if(!empty($image)) {
                    // Resize Image
                    $resize = new Zend_Filter_ImageSize();
                    preg_match('#\.([^\.]+)$#', $image, $matches);
                    $fileExtension  = $matches[1];
                    $fileName = 'pic_' . time() . mt_rand(5, 99999999);
                    $newFileName1 = $fileName . '.' . $fileExtension;
                    
                    $options = array('target' => BASE_PATH . '/public/files/image/' . $newFileName1,'overwrite'=>true);
                    $upload->addFilter('Rename',$options,'image');
                    $upload->receive('image');
                    $output = $resize->setHeight(400)
                                    ->setWidth(500)
                                    ->setThumnailDirectory(BASE_PATH . '/public/files/image/')
                                    ->filter(BASE_PATH . '/public/files/image/' . $newFileName1);
                    header('Content-Type: image/jpeg');
                    $fh = fopen($output, 'r');
                    fpassthru($fh);
                    fclose($fh);
                    $newFileName = $fileName . '-500x400.' . strtolower($fileExtension);
                    unlink(PUBLIC_PATH . '/files/image/' . $newFileName1);
                    unlink(PUBLIC_PATH . '/files/image/' . $this->view->image['image']);
                }
                
                // Insert to database
                $strFlex = new Zendvn_Filter_RemoveCircumflex();
                if(!empty($image)) {
                    $data_image['image'] = $newFileName;
                    $data_image['id'] = $id;
                    $this->_model->updateItem($data_image);
                }
                foreach($name as $key=>$value) {
                    foreach($this->view->language as $lang) {
                        if($key == $lang['locale']) {
                            $data = array(
                                'image_id' => $id,
                                'language_id' => $lang['id'],
                                'name' => $value,
                            );
                            $this->_model->updateItem($data, 'language');
                            break;
                        }
                    }
                }
                $this->_redirect('admincp/image/index?type='.$type.'&hotel_id='.$hotel_id);
            }
        }
        
        public function deleteAction()
        {
            $id = (int) $this->_arrParam['id'];
            $type = (int) $this->_arrParam['type'];
            $hotel_id = (int) $this->_arrParam['hotel_id'];
            $this->_model->deleteItem($id);
            $this->_redirect('admincp/image/index?type='.$type.'&hotel_id='.$hotel_id);
        }
        
        public function publishAction()
        {
            $id = (int) $this->_arrParam['id'];
            $status = (int) $this->_arrParam['status'];
            $status = ($status == 1) ? 2 : 1;
            $data = array(
                'id'=>$id,
                'status'=>$status,
            );
            $this->_model->updateItem($data);
            $this->_redirect($_SERVER['HTTP_REFERER']);
        }
    }
